package com.likou;

import java.util.LinkedList;
import java.util.Queue;

public class q101 {


    // 迭代
    public boolean isSymmetric(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);
        TreeNode left = null;
        TreeNode right = null;

        while (!queue.isEmpty()) {
            left = queue.poll();
            right = queue.poll();
            if (left == null && right == null) {
                continue;
            }
            if (left == null || right == null) {
                return false;
            }
            if (left.val != right.val) {
                return false;
            }

            queue.offer(left.left);
            queue.offer(right.right);
            queue.offer(left.right);
            queue.offer(right.left);
        }

        return true;

    }

//    // 递归
//    public boolean isSymmetric(TreeNode root) {
//        return check(root.left, root.right);
//    }
//
//    private boolean check(TreeNode left, TreeNode right) {
//        if (left == null && right == null) {
//            return true;
//        }
//        if (left == null || right == null) {
//            return false;
//        }
//
//        if (left.val != right.val) {
//            return false;
//        }
//
//        return check(left.left, right.right) && check(left.right, right.left);
//    }
}
